package com.effect.part1_ObjectCrateDestroy.item5;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 避免创建不必要的对象
 * Created by jon on 17-10-7.
 */

//isBabyBoomer每次被调用的时候，都会新建一个Calendar、一个TimeZone和两个Date实例
public class Person_01 {
    private  Date birthDate;

    // Other fields, methods , and constructor omitted
    // DON'T DO THIS!!!
    public boolean isBabyBoomer() {
        //Unnecessary allocation of expensive object
        Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
        Date boomStart = gmtCal.getTime();
        gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
        Date boomEnd = gmtCal.getTime();
        return birthDate.compareTo(boomEnd) >= 0 && birthDate.compareTo(boomEnd) < 0;
    }
}
